package com.twang.stock.controller.old;

import com.twang.stock.entity.CompanyCodeInfo;
import com.twang.stock.entity.QinXuZhiShu;
import com.twang.stock.entity.StockInfo;
import com.twang.stock.dto.BaseRequest;
import com.twang.stock.service.jinyan.QinXUZhiShuService;
import com.twang.stock.service.jinyan.StockInfoService;
import com.twang.stock.task.old.UpdateToDayVol;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping(value = "/stockInfo")
@Slf4j
public class StockInfoController {

    @Autowired
    private StockInfoService stockInfoService;

    @Autowired
    private QinXUZhiShuService qinXUZhiShuService;
    @Autowired
    private UpdateToDayVol updateToDayVol;

    /**
     * 获取数据
     *HTTP http://127.0.0.1:8080/stockInfo/query
     * @return
     */
    @RequestMapping(value = "/query")
    public ModelAndView query(BaseRequest request) throws Exception {
        QinXuZhiShu qinXuZhiShu=qinXUZhiShuService.duanxian().get(0);
        List<StockInfo> list=stockInfoService.queryByPage(request);
        BigDecimal rate=BigDecimal.ZERO;
         for(StockInfo info:list){
             rate=rate.add(info.getNotNullZhangDie());
         }
         if(list.size()>0){
             rate=rate.divide(new BigDecimal(list.size()),2,BigDecimal.ROUND_HALF_UP);
         }else{
             rate=BigDecimal.ZERO;
         }
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("resultList",list);
        modelAndView.addObject("qinXuZhiShu", qinXuZhiShu );
        modelAndView.addObject("rate", rate);
        modelAndView.setViewName("pages/table_data_tables");
        return  modelAndView;
    }

    /**
     * 查询历史重复数据，间隔一次出现
     *
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/getZhangDie")
    @ResponseBody
    public String getZhangDie(String code){
        CompanyCodeInfo companyCodeInfo = new CompanyCodeInfo();
        companyCodeInfo.setCompanyCode(code);
        StockInfo stockInfo=  updateToDayVol.querySina(companyCodeInfo);
        return null==stockInfo.getZhangdie()?"0%":stockInfo.getZhangdie().toString()+"%";
    }
}
